use fluffy::{tmpl::Tpl, db, model::Model};
use actix_web::HttpResponse;
use crate::caches::tags::TAGS;
use crate::models::Videos;

/// 视频列表
pub async fn index(tpl: Tpl) -> HttpResponse { 
    let mut data = tmpl_data![];
    let query = query![
        fields => "id, title, remark, created, cover_image",
        order_by => "seq DESC",
        limit => 12,
    ];
    //let cond = cond![
    //];
    let mut conn = db::get_conn();
    let result = Videos::fetch_rows(&mut conn, &query, None);
    let mut rows = vec![];
    for r in result { 
        let (id, title, remark, created, cover_image): (usize, String, String, u32, String) = from_row!(r);
        rows.push(Videos{id, title, remark, created, tag_ids: "".to_owned(), cover_image});
    }
    data.insert("rows", &rows);
    super::set_navs(&mut data);
    let tags = &*TAGS;
    data.insert("tags", tags);
    data.insert("nav_title", "视频列表");
    render!(tpl, "videos/index.html", &data)
}

/// 视频详情
pub async fn view(tpl: Tpl) -> HttpResponse { 
    let mut data = tmpl_data![];
    super::set_navs(&mut data);
    data.insert("nav_title", "视频详情");
    render!(tpl, "videos/view.html", &data)
}
